package com.bw.shiro.authorization;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.SimpleAccountRealm;
import org.apache.shiro.subject.Subject;
import org.junit.Test;

public class Authorization {
    @Test
    public void testAuthority(){
        //声明一个realm域
        SimpleAccountRealm realm = new SimpleAccountRealm();
        realm.addAccount("zhangsan","123","admin");
        //声明shiro的核心
        DefaultSecurityManager manager = new DefaultSecurityManager();
        manager.setRealm(realm);//设置使用realm
        //声明shiro环境
        SecurityUtils.setSecurityManager(manager);//安全管理
        //声明主体
        Subject subject = SecurityUtils.getSubject();
        //前台的数据
        UsernamePasswordToken token = new UsernamePasswordToken("zhangsan","123");
        //验证是否成功
        try {
            subject.login(token);
        }catch (Exception e){
            e.printStackTrace();
        }
        //是否授权
        System.out.println("授权："+subject.hasRole("admin"));


    }
}
